Page({
  data: {
    input: '',
    todos: [], //列表
    leftCount: 0,
    allCompleted: false,
    logs: []
  },
  //点击跳转到logs页面
  gologs: function(param){
    wx.navigateTo({
  
      url: '/pages/dongtai/logs/logs',
      })
  },
  //本地存储数据
  save: function () {
    wx.setStorageSync('todo_list', this.data.todos);
    wx.setStorageSync('todo_logs', this.data.logs);
  },
  //加载数据
  load: function () {
    var todos = wx.getStorageSync('todo_list')
    if (todos) {
      var leftCount = todos.filter(function (item) {
        return !item.completed
      }).length
      this.setData({ todos: todos, leftCount: leftCount })
    }
    var logs = wx.getStorageSync('todo_logs')
    if (logs) {
      this.setData({ logs: logs })
    }
  },
  // setData 函数用于将数据从逻辑层发送到视图层（异步），同时改变对应的 this.data 的值（同步）
  onLoad: function () {
    this.load();
  },

  inputChangeHandle(e) {
    this.setData({ input: e.detail.value })
  },
  //添加任务
  addTodoHandle: function (e) {
    if (!this.data.input || !this.data.input.trim()){
      wx.showToast({
        title: '请输入内容',
        icon: 'none'
      });
      return;
    }
    var todos = this.data.todos;
    todos.push({ name: this.data.input, completed: false }); //新加一条
    var logs = this.data.logs; //日志
    logs.push({ timestamp: new Date(), action: '新增', name: this.data.input });
    this.setData({
      input: '',
      todos: todos,
      leftCount: this.data.leftCount + 1,
      logs: logs
    })
    this.save(); //本地更新
  },
  //完成任务
  toggleTodoHandle: function (e) {
    var index = e.currentTarget.dataset.index;
    var todos = this.data.todos;
    todos[index].completed = !todos[index].completed;
    var logs = this.data.logs;
    console.log('1', logs);
    logs.push({
      timestamp: new Date(),
      action: todos[index].completed ? '完成' : '重开',
      name: todos[index].name
    });
    console.log('2', logs);
    this.setData({
      todos: todos,
      leftCount: this.data.leftCount + (todos[index].completed ? -1 : 1),
      logs: logs
    });
    this.save()
  },
  //移除任务
  removeTodoHandle(e) {
    var index = e.currentTarget.dataset.index;
    var todos = this.data.todos;
    var remove = todos.splice(index, 1)[0];
    var logs = this.data.logs;
    logs.push({ timestamp: new Date(), action: '被移除', name: remove.name });
    this.setData({
      todos: todos,
      leftCount: this.data.leftCount - (remove.completed ? 0 : 1),
      logs: logs
    });
    this.save();
  },
  toggleAllHandle: function (e) {
    this.data.allCompleted = !this.data.allCompleted
    var todos = this.data.todos
    for (var i = todos.length - 1; i >= 0; i--) {
      todos[i].completed = this.data.allCompleted
    }
    var logs = this.data.logs
    logs.push({
      timestamp: new Date(),
      action: this.data.allCompleted ? 'Finish' : 'Restart',
      name: 'All todos'
    });
    this.setData({
      todos: todos,
      leftCount: this.data.allCompleted ? 0 : todos.length,
      logs: logs
    });
    this.save();
  },
  //清除掉已完成的任务
  clearCompletedHandle: function (e) {
    var todos = this.data.todos
    var remains = []
    for (var i = 0; i < todos.length; i++) {
      todos[i].completed || remains.push(todos[i]);
    }
    var logs = this.data.logs
    logs.push({
      timestamp: new Date(),
      action: 'Clear',
      name: 'Completed todo'
    });
    this.setData({ todos: remains, logs: logs });
    this.save();
  }
})
